"""
系统异常定义
===========
定义系统级别的异常
"""

from .base_exceptions import RedFireException


class DatabaseException(RedFireException):
    """数据库异常"""
    
    def __init__(self, message: str = "数据库操作失败", operation: str = None, table: str = None):
        details = {}
        if operation:
            details['operation'] = operation
        if table:
            details['table'] = table
        
        super().__init__(
            message=message,
            error_code="DATABASE_ERROR",
            details=details
        )


class CacheException(RedFireException):
    """缓存异常"""
    
    def __init__(self, message: str = "缓存操作失败", operation: str = None, key: str = None):
        details = {}
        if operation:
            details['operation'] = operation
        if key:
            details['key'] = key
        
        super().__init__(
            message=message,
            error_code="CACHE_ERROR",
            details=details
        )


class MessagingException(RedFireException):
    """消息队列异常"""
    
    def __init__(self, message: str = "消息队列操作失败", queue: str = None, operation: str = None):
        details = {}
        if queue:
            details['queue'] = queue
        if operation:
            details['operation'] = operation
        
        super().__init__(
            message=message,
            error_code="MESSAGING_ERROR",
            details=details
        )


class StorageException(RedFireException):
    """存储异常"""
    
    def __init__(self, message: str = "存储操作失败", bucket: str = None, key: str = None):
        details = {}
        if bucket:
            details['bucket'] = bucket
        if key:
            details['key'] = key
        
        super().__init__(
            message=message,
            error_code="STORAGE_ERROR",
            details=details
        )


class ExternalServiceException(RedFireException):
    """外部服务异常"""
    
    def __init__(self, message: str = "外部服务调用失败", service: str = None, status_code: int = None):
        details = {}
        if service:
            details['service'] = service
        if status_code:
            details['status_code'] = status_code
        
        super().__init__(
            message=message,
            error_code="EXTERNAL_SERVICE_ERROR",
            details=details
        )


class ConnectionException(RedFireException):
    """连接异常"""
    
    def __init__(self, message: str = "连接失败", host: str = None, port: int = None):
        details = {}
        if host:
            details['host'] = host
        if port:
            details['port'] = port
        
        super().__init__(
            message=message,
            error_code="CONNECTION_ERROR",
            details=details
        )


class TimeoutException(RedFireException):
    """超时异常"""
    
    def __init__(self, message: str = "操作超时", timeout: int = None, operation: str = None):
        details = {}
        if timeout:
            details['timeout'] = timeout
        if operation:
            details['operation'] = operation
        
        super().__init__(
            message=message,
            error_code="TIMEOUT_ERROR",
            details=details
        )


class SerializationException(RedFireException):
    """序列化异常"""
    
    def __init__(self, message: str = "序列化失败", data_type: str = None):
        details = {}
        if data_type:
            details['data_type'] = data_type
        
        super().__init__(
            message=message,
            error_code="SERIALIZATION_ERROR",
            details=details
        )


class HealthCheckException(RedFireException):
    """健康检查异常"""
    
    def __init__(self, message: str = "健康检查失败", service: str = None, check_type: str = None):
        details = {}
        if service:
            details['service'] = service
        if check_type:
            details['check_type'] = check_type
        
        super().__init__(
            message=message,
            error_code="HEALTH_CHECK_ERROR",
            details=details
        )


class RateLimitException(RedFireException):
    """限流异常"""
    
    def __init__(self, message: str = "请求频率超限", limit: int = None, window: int = None):
        details = {}
        if limit:
            details['limit'] = limit
        if window:
            details['window'] = window
        
        super().__init__(
            message=message,
            error_code="RATE_LIMIT_ERROR",
            details=details
        )
